package com.hhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhxy.constants.SystemConstants;
import com.hhxy.handler.exception.CustomException;
import com.hhxy.mapper.LinkMapper;
import com.hhxy.model.dto.LinkDto;
import com.hhxy.model.entity.Link;
import com.hhxy.model.vo.LinkVo;
import com.hhxy.model.vo.PageVo;
import com.hhxy.service.LinkService;
import com.hhxy.utils.BeanCopyUtils;
import com.hhxy.utils.response.AppHttpCodeEnum;
import com.hhxy.utils.response.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author ghp
 * @description 针对表【tb_link(友链)】的数据库操作Service实现
 * @createDate 2023-04-25 11:58:47
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link>
        implements LinkService {

    /**
     * 查询所有的友链
     *
     * @return
     */
    @Override
    public ResponseResult getAllLink() {
        // 查询所有审核通过的友链
        List<Link> linkList = this.list(new LambdaQueryWrapper<Link>()
                .eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL)
                .orderByDesc(Link::getUpdateTime));
        // 封装VO
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    /**
     * 分页条件查询友链
     */
    @Override
    public ResponseResult list(Integer pageNum, Integer pageSize, String name, String status) {
        // 分页查询友链
        Page<Link> page = new Page<>(pageNum, pageSize);
        this.page(page, new LambdaQueryWrapper<Link>()
                .like(StringUtils.hasText(name), Link::getName, name)
                .eq(StringUtils.hasText(status), Link::getStatus, status)
                .orderByDesc(Link::getUpdateTime));
        // 封装VO
        List<LinkVo> linkVoList = BeanCopyUtils.copyBeanList(page.getRecords(), LinkVo.class);
        return ResponseResult.okResult(new PageVo<LinkVo>(linkVoList, page.getTotal()));
    }

    /**
     * 根据id删除友链
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteByIds(Long[] ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids));
        if (!flag){
            throw new CustomException(AppHttpCodeEnum.SERVICE_DELETE_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS_DELETE);
    }

    /**
     * 根据id获取友链
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getLinkById(Long id) {
        // 查询Link
        Link link = this.getById(id);
        // 封装VO
        LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
        return ResponseResult.okResult(linkVo);
    }

    /**
     * 根据id修改友链
     *
     * @param linkDto
     * @return
     */
    @Override
    public ResponseResult updateLinkById(LinkDto linkDto) {
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        boolean flag = this.updateById(link);
        if (!flag){
            throw new CustomException(AppHttpCodeEnum.SERVICE_UPDATE_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS_UPDATE);
    }

    /**
     * 新增友链
     *
     * @param linkDto
     * @return
     */
    @Override
    public ResponseResult add(LinkDto linkDto) {
        Link link = BeanCopyUtils.copyBean(linkDto, Link.class);
        boolean flag = this.save(link);
        if (!flag){
            throw new CustomException(AppHttpCodeEnum.SERVICE_UPDATE_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS_INSERT);
    }

}




